<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/**
 *  Advertising Model Class
 *
 * @package XGO CMS v2.0
 * @subpackage News
 * @author Manh Nguyen - 03/08/2012
 * @link http://sunsoft.vn
*/
class Statistics_model extends DataMapper {

	var $model = 'statistics_model';
	var $table = 'statistics';

	// --------------------------------------------------------------------
	// Construct
	// --------------------------------------------------------------------
	public function __construct() {
		parent::__construct();
		$this->active = 1;
	}

	// --------------------------------------------------------------------
	// Relationships
	// --------------------------------------------------------------------
	var $has_one = array();
	var $has_many = array();
	// --------------------------------------------------------------------
	// Validation
	// --------------------------------------------------------------------
	var $validation = array();

	/**
	 * Default order in query select
	*/
	var $default_order_by = array('order');

	/**
	 * string value of object
	 *
	 * @access private
	 * @param null
	 * @return string
	*/
	function __toString() {
		return empty($this->title) ? $this->localize_label('unset') : $this->title;
	}

	function build_view(&$view){

		$view->series_data_news="[";
			

		$news_obj 			= new News_model();
		$seria_data_news 	= $news_obj->statistics_by_category();
		//     	[
		//     	['Chưa kích hoạt',   45.0],
		//     	{
		//     		name: 'Kích hoạt',
		//     		y: 12.8,
		//     		sliced: true,
		//     		selected: true
		//     	}
		//     	]
			
		$view->total_news=0;
		foreach($seria_data_news['normal'] as $k=>$item){
			$view->series_data_news.="['{$item}',   {$k}],";
			$view->total_news+=$k;
		}
			
		if(isset($seria_data_news['important'])){
			foreach($seria_data_news['important'] as $k=>$item){
				$view->series_data_news.="{
				name: '{$item}',
				y: {$k},
				sliced: true,
				selected: true
			},";
				$view->total_news+=$k;

			}
		}
		$view->series_data_news.="]";

		//$view->series_data_advertising="[";
		$advertising_obj 			= new Advertising_model();
		$seria_data_advertising 	= $advertising_obj->statistics_by_position();
		//     				{
		// 		                name: 'Chưa kích hoạt',
		// 		                data: [5, 3]
		// 		            }, {
		// 		                name: 'Kích hoạt',
		// 		                data: [2, 2]
		// 		            }
		//['Vị trí 1', 'Vị trí 2']
		$view->total_advertising = 0;
		$view->series_data_advertising_disable="{name: 'Chưa kích hoạt', data: [";
		$view->series_data_advertising_active="{name: 'Kích hoạt', data: [";
		foreach($seria_data_advertising as $k=>$item){
			$k = ucfirst($k);
			$view->series_category_advertising .= "'{$k}', ";

			$item[1]=isset($item[1])?$item[1]:0;
			$item[0]=isset($item[0])?$item[0]:0;
			$view->series_data_advertising_disable.=$item[0].",";
			$view->series_data_advertising_active.=$item[1].",";

			$view->total_advertising+=$item[0]+$item[1];
		}
		$view->series_data_advertising_disable.="]}";
		$view->series_data_advertising_active.="]}";
			
		//     	['Chưa kích hoạt',   45.0],
		//     	{
		//     		name: 'Kích hoạt',
		//     		y: 12.8,
		//     		sliced: true,
		//     		selected: true
		//     	}
			
			
		$view->series_data_data="[";

		$data_obj 			= new Data_model();
		$seria_data_data 	= $data_obj->statistics_by_type();
			
		$view->total_data = 0;
		foreach($seria_data_data as $k=>$item){
			$view->total_data+=$item;
			$k = ucfirst($k);
			$view->series_data_data.="['{$k}',   {$item}],";
		}

		//     	foreach($seria_data_data['important'] as $k=>$item){
		//     		$view->series_data_data.="{
		// 	    		name: '{$item}',
		// 	    		y: {$k},
		// 	    		sliced: true,
		// 	    		selected: true
		// 	    	},";
		// 	    	$view->total_data+=$k;
		//     	}
			
		$view->series_data_data.="]";
			
			
		//other statistics
		//     	$other_obj = array(Category_model()", "Menu_model()", "Configuration_model()", "User_model()", "Group_model()", "Role_model()");

		//     	foreach($other_obj as $item){
		//     		$obj = new $item;
		//     		$view->other_statistics[]	=	$obj->total();
		//     	}
		//

		$obj 						= new Category_model();
		$view->other_statistics[1]	=	$obj->total();

		$obj 						= new Menu_model();
		$view->other_statistics[2]	=	$obj->total();
			
		$obj 						= new Configuration_model();
		$view->other_statistics[3]	=	$obj->total();
			
		$obj 						= new User_model();
		$view->other_statistics[4]	=	$obj->total();

		$obj 						= new Group_model();
		$view->other_statistics[5]	=	$obj->total();

		$obj 						= new Role_model();
		$view->other_statistics[6]	=	$obj->total();

		$cat_id = new Category_model();
		$cat_id = $cat_id->get_all_id();
		foreach($cat_id as $item)
		{
			$arr_cat[] = $item->id;
		}
		$obj 						= new News_model();
		$view->news_top_last		= $obj->get_last_news($arr_cat,10);

		$obj 						= new News_model();
		$view->news_top_hits		= $obj->get_most_view(10);

		$query = $this->db->query("select count(*) as count from sessions");
		foreach($query->result() as $item){
			$view->online['total'] = $item->count;
		}

		// 		$query = $this->db->query("select user_data, ip_address from sessions");
		// 		$session_user_data_regex =  '/(.+)s:15:"(.+)";s:12:"logged_in_id";i:([0-9]+);(.+)?}/';

		// 		//backend only
		// 		$session_extract = array();
		// 		foreach($query->result() as $item){
		// 			preg_match($session_user_data_regex, $item->user_data, $n);
		// 			if(isset($n[3])){
		// 				$session_extract['user_id'][$n[3]] 		= $n[3];
		// 				$session_extract['visting'][$n[3]] 		= $n[2];
		// 				$session_extract['ip_address'][$n[3]] 	= $item->ip_address;
		// 			}
		// 		}


		// 		$user_obj 					= new User_model();
		// 		$user_name					= $user_obj->get_user_name($session_extract['user_id']);

		// 		foreach($user_name as $item){
		// 			$view->online['children'][] 	= array(
		// 					'user_name' 	=> 	$item->username,
		// 					'visiting'		=>	$session_extract['visting'][$item->id],
		// 					'ip_address'	=> 	$session_extract['ip_address'][$item->id],
		// 			);
		// 		}

		// 		$view->online['total'] = count($session_extract['visting']);

		// */
		// 		  		print_r($view);
		// 		  		exit;
	}
}

/* End of file advertising_model.php */
/* Location: ./application/models/advertising_model.php */